package com.ftdigital.primemovers_operators.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.ftdigital.primemovers_operators.MyApplication;
import com.ftdigital.primemovers_operators.config.GlobalData;
import com.ftdigital.primemovers_operators.fragment.TradingDetailFragment;
import com.ftdigital.primemovers_operators.util.http.RetryHandler;

import org.apache.http.impl.client.TunnelRefusedException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * Created by Curry on 2016-1-27.
 */
public class CacheManager {

    private static long cacheTime = 0 * 60 * 1000;


    public static Serializable readObject(Context context, String cacheKey) {

        FileInputStream fileInputStream = null;
        ObjectInputStream objectInputStream = null;
        if (!isFileExist(context, cacheKey)) {  //判断文件是否存在
            return null;
        }
        try {
            fileInputStream = context.openFileInput(cacheKey);
            objectInputStream = new ObjectInputStream(fileInputStream);
            return (Serializable) objectInputStream.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                objectInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

//    public static boolean saveObject(Context context, Serializable serializable, String cacheFile) {
//        FileOutputStream fileOutputStream = null;
//        ObjectOutputStream objectOutputStream = null;
//        try {
//            fileOutputStream = context.openFileOutput(cacheFile, Context.MODE_PRIVATE);
//            objectOutputStream = new ObjectOutputStream(fileOutputStream);
//            objectOutputStream.writeObject(serializable);
//            objectOutputStream.flush();
//            return true;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return false;
//        } finally {
//            try {
//                objectOutputStream.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            try {
//                fileOutputStream.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//
//
//    }

    /**
     * 保存对象
     *
     * @param ser
     * @param file
     * @throws IOException
     */
    public static boolean saveObject(Context context, Serializable ser,
                                     String file) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = context.openFileOutput(file, Context.MODE_PRIVATE);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(ser);
            oos.flush();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                oos.close();
            } catch (Exception e) {
            }
            try {
                fos.close();
            } catch (Exception e) {
            }
        }
    }

    public static boolean isFileExist(Context context, String cacheFile) {
        if (context == null) {
            return false;
        }
        File data = context.getFileStreamPath(cacheFile);
        if (data.exists()) {
            return true;
        } else {
            return false;
        }
    }

    //判断文件是否失效
    public static boolean isCacheDataFaile(Context context, String cacheFile) {
        if (context == null) {
            Log.i(GlobalData.TAG, "context == null");
            return true;
        }
        File data = context.getFileStreamPath(cacheFile);
        if (!data.exists()) {
            Log.i(GlobalData.TAG, "!data.exists()");
            return true;
        }
        long existTime = System.currentTimeMillis() - data.lastModified();
        //附加功能判断是wifi还是数据网络
        return existTime > cacheTime ? true : false;
    }

}
